var isVoted =false;
setCaptchaId = function(result) {
      document.getElementById("challenge").value = result;
  };

getCaptchaId = function() {
    var script = document.createElement("script");
    script.type = 'text/javascript';
    script.src = "http://captcha5.pbs.qunar.com/api/getCaptchaId?callback=setCaptchaId&t="
            + Math.random();

    var heads = document.getElementsByTagName("head");
    var container = heads ? heads[0] : document.documentElement;
    container.insertBefore(script, container.firstChild);
}

$(document).ready(function() {
  var overLayerHeight = $('body').height();
  var winHeight = $(window).height();
  $('.btn_vote').click(function() {
    if (isVoted) {
      switchImage();
    }
    var winScrollTop = $(window).scrollTop();
    $('#overLayer').show().height(overLayerHeight);
    $('.popup').show().css('top', (((winHeight - $('.popup').height()) / 2) + winScrollTop) + 'px');
    var link_val = this.name; 
      
    $('.popup').undelegate().delegate('#okBtn', 'click', function(ev) {
      var winScrollTop = $(window).scrollTop();
      if (!$.trim($('#vCode').val()) == '') {
        function callbackFn(data) {
          if(data) {
            if (parseInt(data.ret, 10)) {
              alert('您输入的验证码不正确，请重新输入！');
            } else {
              $('.popup').hide();
              if (!parseInt(data.status, 10)) {
                $('.success_box').show().css('top', (((winHeight - $('.success_box').height()) / 2) + winScrollTop) + 'px');
                $('.success_box').delegate('.close', 'click', function(ev) {
                  $('.success_box').hide();
                  $('#overLayer').hide();
                  return false;
                });
                var target = $('#vc_' + data.data);
                target.text(parseInt(target.text()) + 1);
              } else {
                alert(data.message);
              }
            }
          }
          $('#overLayer').hide();
        }
        try {
          $.ajax({
            url: 'vote.htm',
            type: 'post',
            data: {
            'link': link_val,
            'captcha': $('#vCode').val()
            },
            dateType: 'json',
            success: callbackFn,
            error: function() {
              alert('不好意思，出错了');
            }
          });
        } catch(ex) {}
      }
      return false;
    });
    isVoted = true;	
  });
    $('#btn_login').click(function() {
        var username=$('input#lg_name').val();
        var password = $('input#lg_pass').val();
        if (username=='') {
            alert('请输入登陆名');
            $('input#lg_name').focus();
            return false;
        }
        if (password=='') {
            alert('请输入登陆密码');
            $('input#lg_pass').focus();
            return false;
        }
        $.post('login.htm', {
            "username" : username,
            "password" : password
        }, function(resp) {
            if (resp.status==0) {
                window.location.href='profile.htm';
            } else {
                alert(resp.message);
            }
        });
        return false;
    });

    $('input#btn_sign').click(function() {
        var code = $('input#code').val();
        var name = $('input#name').val();
        var certNo = $('input#certNo').val();
        var school = $('input#school').val();
        var major = $('input#major').val();
        var grade = $('input#grade').val();
        var mobile = $('input#mobile').val();
        var email = $('input#email').val();
        var username = $('input#username').val();
        var password = $('input#password').val();
        var repassword = $('input#repassword').val();
        var comment=$('#comment').val();
        var avatar=$('input#hide_avatar').val();

        if (code=='') {
            alert("报名序号不能为空");
            $('#code').focus();
            return;
        }
        if (name=='' || name=='仅为汉字，长度为8个字以内') {
            alert("姓名不能为空");
            $('input#name').focus();
            return;
        }
        if (certNo=='') {
            alert("身份证号不能为空");
            $('input#certNo').focus();
            return;
        }
         if (school=='') {
            alert("学校不能为空");
            $('input#school').focus();
            return;
        }
         if (major=='') {
            alert("专业不能为空");
            $('input#major').focus();
            return;
        }
         if (grade=='') {
            alert("年级不能为空");
            $('input#grade').focus();
            return;
        }
         if (mobile=='') {
            alert("手机号码不能为空");
            $('input#mobile').focus();
            return;
        }
         if (email=='') {
            alert("电子邮件不能为空");
            $('input#email').focus();
            return;
        }
        
         if (username=='') {
            alert("用户名不能为空");
            $('input#username').focus();
            return;
           } 
        if (password=='') {
            alert("密码不能为空");
            $('input#password').focus();
            return;
        }
        if (repassword != password) {
            alert("两次输入密码不一致");
            $('#repassword').focus();
            return;
        }
        if (avatar == '') {
            alert("请上传头像");
            $('#btn_broswer').focus();
            return;
        }
        if (!$('#license').prop("checked")) {
            alert("请选中同意以上条款后提交");
            $('#license').focus();
            return;
        }

        $.post('sign.htm', {
        	"code" : code,
        	"name" : name,
            "certNo" : certNo,
            "school" : school,
            "major" : major,
            "grade" : grade,
            "mobile" : mobile,
            "email" : email,
            "username" : username,
            "password" : password,
            "repassword" : repassword,
            "comment" : comment,
            "avatar" : avatar
        }, function(resp) {
            if (resp.status==0) {
                alert(resp.data);
                window.location.href='home.htm';
            } else {
                alert(resp.message);
            }
        });
    });

    $('button#btn_edit').click(function() {
        var mobile = $('input#mobile').val();
        var email = $('input#email').val();
        var password = $('input#password').val();
        var repassword = $('input#repassword').val();
        var comment=$('#comment').val();
        var avatar=$('input#hide_avatar').val();

        if (mobile=='') {
            alert("手机号码不能为空");
            $('input#mobile').focus();
            return;
        }
        if (email=='') {
            alert("电子邮件不能为空");
            $('input#email').focus();
            return;
        }
        if (password!='') {
	        if (repassword != password) {
	            alert("两次输入密码不一致");
	            $('#repassword').focus();
	            return;
	        }
        }
        if (avatar == '') {
            alert("请上传头像");
            $('#btn_broswer').focus();
            return;
        }

        $.post('edit.htm', {
            "mobile" : mobile,
            "email" : email,
            "password" : password,
            "repassword" : repassword,
            "comment" : comment,
            "avatar" : avatar
        }, function(resp) {
            if (resp.status==0) {
                alert(resp.data);
                window.location.href='profile.htm';
            } else {
                alert(resp.message);
            }
        });
    });


    $('button#btn_report').click(function() {
    	var name = $('input#name').val();
        var mobile = $('input#mobile').val();
        var code = $('input#code').val();
        var cname = $('input#cname').val();
        var content=$('#content').val();

        if (name=='') {
            alert("请填写您的姓名。");
            $('input#name').focus();
            return;
        }
        if (mobile=='') {
            alert("请填写你的电话号码，以便我们能和你联系。");
            $('input#mobile').focus();
            return;
        }
        if (code=='') {
            alert("请填写被投诉者报名序号。");
            $('input#code').focus();
            return;
        }
        if (content == '') {
            alert("请填写投诉内容。");
            $('#content').focus();
            return;
        }

        $.post('report.htm', {
        	"name" : name,
            "mobile" : mobile,
            "code" : code,
            "cname" : cname,
            "content" : content
        }, function(resp) {
            if (resp.status==0) {
                alert(resp.data);
                window.location.href='home.htm';
            } else {
                alert(resp.message);
            }
        });
    });
});

$(function() {
    var path = window.location.pathname;
    var s = path.lastIndexOf('/');
    var e = path.lastIndexOf('.');
    var m_name = '#m_' + path.substring(s+1, e);
    $(m_name).addClass("active");
});
